import axios from 'axios';
import constants from '../utils/constants';
// import router from "@/router";
// import store from "@/store";

const getToken = function() {
    return window.localStorage.getItem(constants.ACCESS_TOKEN) || "";
}

axios.defaults.baseURL = `${process.env.VUE_APP_CKZL_TEST}/api`;
axios.defaults.withCredentials = true;

axios.interceptors.request.use(
    config => {
        console.log("token","卡卡"+getToken()+"kka")
        config.headers[constants.ACCESS_TOKEN] = getToken();
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

axios.interceptors.response.use(
    response => {
        console.log("执行3",response)
        return response;
    },
    err => {
        // 请求错误做点什么
        console.log("请求错误做点什么", err);
        if (err) {
            let data = err.data
            const token = getToken();
            console.log("------异常响应------", token)
            console.log("------异常响应------", data.status)
            switch (data.code) {
                case 403:
                    console.error('拒绝访问');
                    break;
                case 500:
                    /*if (!token || data.message == "Token失效，请重新登录") {
                        let timeout = setTimeout(tip.alert('登录已过期'), 1000);
                        store.dispatch('Logout').then(() => {
                            clearTimeout(timeout)
                            window.location.reload()
                        })
                    }*/
                    break;
                case 404:
                    break;
                case 504:
                    break;
                case 401:

                    if (token) {

                        /* store.dispatch('Logout').then(() => {
                           setTimeout(() => {
                             window.location.reload()
                           }, 1500)
                         }) */
                    }
                    break
                default:
                    console.error({
                        duration: 0,
                        forbidClick: true,
                        message: data.message
                    });
                    break;
            }
        }
        return err;
    }
)

export default axios;
